//
//  WorkBenchViewController.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/4/7.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit

class WorkBenchViewController: SYBaseViewController {

    var bgView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.white
        v.layer.cornerRadius = 10.autoWidth()
        return v
    }()
    
    let bgImgView: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "work_bg")
        return v
    }()
    
    let topImgView: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "nav_bg")
        return v
    }()
    
    let titleLb: UILabel = {
        let v = UILabel()
        v.text = "叁拾叁传感器管理平台"
        v.font = 17.autoFontSize()
        v.textColor = UIColor.white
        v.textAlignment = .center
        return v
    }()
    
    let tipText: UILabel = {
        let v = UILabel()
        v.text = "常用应用"
        v.font = 18.autoFontSize()
        v.textColor = UIColor(hex: 0x333333)
        return v
    }()
    
    var fir: GZTitemView = {
        let v = GZTitemView()
        v.imteBt.setImage(UIImage(named: "work_obj"), for: .normal)
        v.arrow.image = UIImage(named: "work_bnext")
        v.title.text = "项目管理(个)"
        return v
    }()
    
    var sec: GZTitemView = {
        let v = GZTitemView()
        v.imteBt.setImage(UIImage(named: "work_equipment"), for: .normal)
        v.arrow.image = UIImage(named: "work_gnext")
        v.title.text = "设备管理(个)"
        return v
    }()
    
    var thir: GZTitemView = {
        let v = GZTitemView()
        v.imteBt.setImage(UIImage(named: "work_port"), for: .normal)
        v.arrow.image = UIImage(named: "work_onext")
        v.title.text = "端口管理(个)"
        return v
    }()
    
    var four: GZTitemView = {
        let v = GZTitemView()
        v.imteBt.setImage(UIImage(named: "work_operations"), for: .normal)
        v.arrow.image = UIImage(named: "work_ynext")
        v.title.text = "运维记录(条)"
        return v
    }()
    
    var five: GZTitemView = {
        let v = GZTitemView()
        v.imteBt.setImage(UIImage(named: "work_equipment"), for: .normal)
        v.arrow.image = UIImage(named: "work_ynext")
        v.title.text = "协议总数(条)"
        return v
    }()
    var six: GZTitemView = {
        let v = GZTitemView()
        v.imteBt.setImage(UIImage(named: "work_port"), for: .normal)
        v.arrow.image = UIImage(named: "work_ynext")
        v.title.text = "预警总数(条)"
        return v
    }()
//    var bottomView: UIView = {
//        let v = UIView()
//        v.backgroundColor = .clear
//        return v
//    }()
    
    var bottomView: UIScrollView = {
        let v = UIScrollView()
        v.isScrollEnabled = true
        return v
    }()
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden = true
        self.tabBarController?.tabBar.isHidden = false
        loadData()
    }
    
    
 
    override func viewDidLoad() {
        super.viewDidLoad()
//        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        
        
//        let scollerView = UIScrollView(frame: CGRect(x: 0, y: 190.autoWidth() + NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - 190.autoWidth() - NAV_HEIGHT - BOTTOM_SAFE_HEIGHT))
//        scollerView.contentSize = bottomView.bounds.size
//        scollerView.isScrollEnabled = true
//
//        scollerView .addSubview(bottomView)
//        view .addSubview(scollerView)
        initViews()
        loadData()
        fir.zaixian.isHidden = true
        fir.lixian.isHidden = true
        four.zaixian.isHidden = true
        four.lixian.isHidden = true
        fir.imteBt.addAction {[weak self] in
            if let `self` = self {
                let vc = CGQxiangmuListViewController()
                self.sy_push(vc)
            }
        }
        sec.imteBt.addAction {[weak self] in
            if let `self` = self {
                
                let vc = CGQShebeiListViewController(model: NSRecordsModel())
                self.sy_push(vc)
            }
        }
        
        thir.imteBt.addAction {[weak self] in
            if let `self` = self {
                let vc = CGQduankouListViewController()
                self.sy_push(vc)
                
            }
        }
        four.imteBt.addAction {[weak self] in
            if let `self` = self {
                
                let vc = CGQyunweiViewController()
                self.sy_push(vc)
            }
        }
        five.imteBt.addAction {[weak self] in
            if let `self` = self {
                
                let vc = CGQxieyiContentViewController()
                self.sy_push(vc)
            }
        }
        six.imteBt.addAction {[weak self] in
            if let `self` = self {
                
                let vc = CGQyujingViewController(typeNum:"2")
                self.sy_push(vc)
            }
        }
        
    }
    
    
    func loadData() {
        
        
        
        requestVM.baseRequest(disposeBag: dispose, type: .shouye, modelClass: SGBaseModel<CGQshouyeModel>.self) {[weak self] (res) in
            
            if let `self` = self  {
                
                self.fir.num.text =  "\(res.data?.appCount ?? 0)"
                self.sec.num.text = "\(res.data?.deviceCount ?? 0)"
                self.thir.num.text = "\(res.data?.portCount ?? 0)"
                self.four.num.text = "\(res.data?.operationCount ?? 0)"
                self.sec.zaixian.text = String(format: "在线：%ld", res.data?.onlineCount ?? 0)
                self.sec.lixian.text = String(format: "离线：%ld", res.data?.offlineCount ?? 0)
                self.thir.zaixian.text = String(format: "打开：%ld", res.data?.portOpenCount ?? 0)
                self.thir.lixian.text = String(format: "关闭：%ld", res.data?.portCloseCount ?? 0)
                self.five.num.text = "\(res.data?.protocolCount ?? 0 )"
                self.six.num.text = "\(res.data?.warningCount ?? 0)"
                self.six.lixian.text = String(format: "未处理：%ld", res.data?.notDealWarningCount ?? 0)
                self.six.zaixian.text = String(format: "已处理：%ld", res.data?.dealWaringCount ?? 0)

     
            }
            
        } Error: {
            
        }
    }
    
    
    
    
    func initViews() {
 
        
        view.addSubview(topImgView)
        view.addSubview(bgImgView)
        view.addSubview(bgView)
        view.addSubview(titleLb)

        view.addSubview(bottomView)
        bottomView.addSubview(tipText)
        bottomView.addSubview(fir)
        bottomView.addSubview(sec)
        bottomView.addSubview(thir)
        bottomView.addSubview(four)
        bottomView.addSubview(five)
        bottomView.addSubview(six)
        topImgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(NAV_HEIGHT)
        }
        bgImgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(NAV_HEIGHT)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(SCREEN_HEIGHT - NAV_HEIGHT)
        }
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(170.autoWidth()+NAV_HEIGHT)
            make.left.equalToSuperview().offset(10.autoWidth())
            make.right.equalToSuperview().offset(-10.autoWidth())
            make.height.equalTo(294.autoWidth())
        }
        titleLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(16.autoWidth() + STATUSBAR_HEIGHT)
            make.left.equalToSuperview().offset(10.autoWidth())
            make.right.equalToSuperview().offset(-10.autoWidth())
            make.height.equalTo(16.autoWidth())
        }
        
        bottomView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(190.autoWidth()+NAV_HEIGHT)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.width.equalTo(SCREEN_WIDTH)
//            make.height.equalTo(120.autoWidth() * 3)
            make.bottom.equalToSuperview().offset(-BOTTOM_SAFE_HEIGHT-44.autoWidth())
        }
        tipText.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(0)
            make.left.equalToSuperview().offset(26.autoWidth())
            make.right.equalToSuperview().offset(-26.autoWidth())
            make.height.equalTo(17.autoWidth())
        }
        fir.snp.makeConstraints { (make) in
            make.top.equalTo(tipText.snp.bottom).offset(25.autoWidth())
            make.left.equalToSuperview().offset(25.autoWidth())
            make.height.equalTo(120.autoWidth())
            make.width.equalTo(157.autoWidth())
        }
        sec.snp.makeConstraints { (make) in
            make.top.equalTo(tipText.snp.bottom).offset(25.autoWidth())
            make.right.equalToSuperview().offset(-25.autoWidth())
            make.left.equalTo(fir.snp.right).offset(10.autoWidth())
            make.height.equalTo(120.autoWidth())
            make.width.equalTo(157.autoWidth())
        }
        thir.snp.makeConstraints { (make) in
            make.top.equalTo(fir.snp.bottom).offset(1)
            make.left.equalToSuperview().offset(25.autoWidth())
            make.height.equalTo(120.autoWidth())
            make.width.equalTo(157.autoWidth())
        }
        four.snp.makeConstraints { (make) in
            make.top.equalTo(thir.snp.top).offset(0)
            make.right.equalToSuperview().offset(-25.autoWidth())
            make.height.equalTo(120.autoWidth())
            make.left.equalTo(thir.snp.right).offset(10.autoWidth())

            make.width.equalTo(157.autoWidth())
        }
        five.snp.makeConstraints { (make) in
            make.top.equalTo(thir.snp.bottom).offset(1)
            make.left.equalToSuperview().offset(25.autoWidth())
            make.height.equalTo(120.autoWidth())
            make.width.equalTo(157.autoWidth())
            make.bottom.equalToSuperview().offset(-5.autoWidth())
        }
        six.snp.makeConstraints { (make) in
            make.top.equalTo(four.snp.bottom).offset(0)
            make.right.equalToSuperview().offset(-25.autoWidth())
            make.height.equalTo(120.autoWidth())
            make.left.equalTo(five.snp.right).offset(10.autoWidth())
            make.width.equalTo(157.autoWidth())
            make.bottom.equalToSuperview().offset(-5.autoWidth())

        }
    }
  

}


class GZTitemView: SYBaseView {
    
    var imteBt: UIButton = {
        let v = UIButton()
        
        return v
    }()
    
    let title: UILabel = {
        let v = UILabel()
        
        v.font = 15.autoFontSize()
        v.textColor = UIColor.white
        return v
    }()
    
    let num: UILabel = {
        let v = UILabel()
        v.font = 30.autoBoldFontSize()
        v.textColor = UIColor.white
        return v
    }()
    
    let arrow: UIImageView = {
        let v = UIImageView()
        
        return v
    }()
    
    let zaixian: UILabel = {
        let v = UILabel()
        v.text = ""
        v.textColor = .white
        v.font = 12.autoFontSize()
        v.textAlignment = .center

        return v
    }()
    
  
    let lixian: UILabel = {
        let v = UILabel()
        v.text = ""
        v.textColor = .white
        v.font = 12.autoFontSize()
        v.textAlignment = .center
        return v
    }()
    
    override func initViews() {
        
        self.addSubview(imteBt)
        self.addSubview(title)
        self.addSubview(num)
        self.addSubview(arrow)
        self.addSubview(zaixian)
        self.addSubview(lixian)
        imteBt.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        title.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(19.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.height.equalTo(15.autoWidth())
        }
        num.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(47.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.height.equalTo(22.autoWidth())
        }
        arrow.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(33.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(25.autoWidth())
            make.width.equalTo(25.autoWidth())
        }
        
        zaixian.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-20.autoWidth())
            make.right.equalToSuperview().offset(-75.autoWidth())
            make.left.equalToSuperview().offset(10.autoWidth())
            make.height.equalTo(12.autoWidth())
        }
     
        lixian.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-20.autoWidth())
            make.right.equalToSuperview().offset(-20.autoWidth())
            make.left.equalTo(zaixian.snp.right).offset(1)
            make.height.equalTo(12.autoWidth())
        }
        
        
    }
    
    
    
    
}
